﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ModemGSM;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmAlarms : FrmDefault
    {
        private bool[] AlarmsPhone = new bool[5];
        private int[] AlarmsRepeate = new int[5];
        public FrmAlarms()
        {
            InitializeComponent();
        }

        private void BantCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmAlarms_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 6; i++)
            {

                ComboBox Cbo = (ComboBox)this.PnlFrm.Controls.Find("CboRep" + i.ToString(), false)[0];
                Cbo.SelectedIndex = 0;
            }
            LoadAlarms();
        }
        private void LoadAlarms()
        {
            this.Cursor = Cursors.WaitCursor;
            List<GSMAlarm> Alarms = SharedLib.ModemGSM.ReadAlarms();
            foreach (var alarm in Alarms)
            {
                AlarmsPhone[alarm.Index] = true;
                int i = alarm.Index+1;
                PictureBox Ctrl = (PictureBox)this.PnlFrm.Controls.Find("PctAlm" + i.ToString(), false)[0];
                Ctrl.Tag = "On";
                Ctrl.Image = Properties.Resources.On;
                Label Sem;
                if (alarm.Sun)
                {
                    Sem = (Label)this.PnlFrm.Controls.Find("LblSun" + i.ToString(), false)[0];
                    Sem.BackColor = Color.FromArgb(192, 255, 192);
                }
                if (alarm.Mon)
                {
                    Sem = (Label)this.PnlFrm.Controls.Find("LblMon" + i.ToString(), false)[0];
                    Sem.BackColor = Color.FromArgb(192, 255, 192);
                }
                if (alarm.Tus)
                {
                    Sem = (Label)this.PnlFrm.Controls.Find("LblTus" + i.ToString(), false)[0];
                    Sem.BackColor = Color.FromArgb(192, 255, 192);
                }
                if (alarm.Wed)
                {
                    Sem = (Label)this.PnlFrm.Controls.Find("LblWen" + i.ToString(), false)[0];
                    Sem.BackColor = Color.FromArgb(192, 255, 192);
                }
                if (alarm.Thu)
                {
                    Sem = (Label)this.PnlFrm.Controls.Find("LblThu" + i.ToString(), false)[0];
                    Sem.BackColor = Color.FromArgb(192, 255, 192);
                }
                if (alarm.Fri)
                {
                    Sem = (Label)this.PnlFrm.Controls.Find("LblFri" + i.ToString(), false)[0];
                    Sem.BackColor = Color.FromArgb(192, 255, 192);
                }
                if (alarm.Sat)
                {
                    Sem = (Label)this.PnlFrm.Controls.Find("LblSat" + i.ToString(), false)[0];
                    Sem.BackColor = Color.FromArgb(192, 255, 192);
                }
                NumericUpDown updn = (NumericUpDown)this.PnlFrm.Controls.Find("UpDownHr" + i.ToString(), false)[0];
                updn.Value = alarm.Hours;
                updn = (NumericUpDown)this.PnlFrm.Controls.Find("UpDownMn" + i.ToString(), false)[0];
                updn.Value = alarm.Minutes;
                ComboBox Cbo = (ComboBox)this.PnlFrm.Controls.Find("CboRep" + i.ToString(), false)[0];
                switch (alarm.Repeat)
                {
                    case RepeatAlamrEnum.Once:
                        Cbo.SelectedIndex = 0;
                        break;
                    case RepeatAlamrEnum.EveryDay:
                        Cbo.SelectedIndex = 1;
                        break;
                    case RepeatAlamrEnum.Days:
                        Cbo.SelectedIndex = 2;
                        break;
                }
                AlarmsRepeate[int.Parse(i.ToString())-1] = Cbo.SelectedIndex;
            }
            this.Cursor = Cursors.Default;
        }
        private void LblweekDay_Click(object sender, EventArgs e)
        {
            Label LblWd = (Label)sender;
            if (LblWd.BackColor == Color.LightGray)
            {
                LblWd.BackColor = Color.FromArgb(192, 255, 192);
            }
            else
            {
                LblWd.BackColor = Color.LightGray;
            }
        }

        private void AlarmOnOff_Click(object sender, EventArgs e)
        {
            string i = ((Control)sender).Tag.ToString();
            PictureBox Ctrl = (PictureBox)this.PnlFrm.Controls.Find("PctAlm" + i.ToString(), false)[0];
            if (Ctrl.Tag.ToString().ToLower() == "off")
            {
                Ctrl.Tag = "On";
                Ctrl.Image = Properties.Resources.On;
            }
            else
            {
                Ctrl.Tag = "Off";
                Ctrl.Image = Properties.Resources.Off;
                ComboBox Cbo = (ComboBox)this.PnlFrm.Controls.Find("CboRep" + i.ToString(), false)[0];
                Cbo.SelectedIndex = 0;
            }
        }

        private void BntSave_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (!SharedLib.ModemGSM.Online)
            {
                this.Cursor = Cursors.Default;
                SharedLib.MsgAlert(ResourcesText.MsgErrOffLine);
                return;
            }
            SharedLib.StartWaitProcessThread();
            for (int i = 1; i < 6; i++)
            {
                int PosAlm = i - 1;
                PictureBox Ctrl = (PictureBox)this.PnlFrm.Controls.Find("PctAlm" + i.ToString(), false)[0];
                if (Ctrl.Tag.ToString().ToLower() == "off")
                {
                    SharedLib.ShowMsgApp(ResourcesText.TurnOffAlarm + " " + PosAlm.ToString() + "...");
                    if (AlarmsPhone[PosAlm])
                    {
                        bool result = SharedLib.ModemGSM.AlarmsDelete(PosAlm);
                        if (result)
                        {
                            SharedLib.ShowMsgApp("");
                        }
                        else
                        {
                            SharedLib.ShowMsgApp(SharedLib.ModemGSM.Error);
                            break;
                        }
                    }
                    else
                    {
                        SharedLib.ShowMsgApp("");
                    }
                }
                else
                {
                    SharedLib.ShowMsgApp(ResourcesText.TurnOnAlarm + " " + PosAlm.ToString() + "...");
                    GSMAlarm Alarm = new GSMAlarm();
                    Alarm.Index = PosAlm;
                    ComboBox Cbo = (ComboBox)this.PnlFrm.Controls.Find("CboRep" + i.ToString(), false)[0];
                    if (AlarmsRepeate[int.Parse(i.ToString())-1] == 0)
                    {
                        Alarm.Repeat = RepeatAlamrEnum.Once;
                    }
                    if (AlarmsRepeate[int.Parse(i.ToString())-1] == 1)
                    {
                        Alarm.Repeat = RepeatAlamrEnum.EveryDay;
                    }
                    if (AlarmsRepeate[int.Parse(i.ToString())-1] == 2)
                    {
                        Alarm.Repeat = RepeatAlamrEnum.Days;
                    }
                    Label Sem = (Label)this.PnlFrm.Controls.Find("LblSun" + i.ToString(), false)[0];
                    if (Sem.BackColor != Color.LightGray)
                    {
                        Alarm.Sun = true;
                    }
                    Sem = (Label)this.PnlFrm.Controls.Find("LblMon" + i.ToString(), false)[0];
                    if (Sem.BackColor != Color.LightGray)
                    {
                        Alarm.Mon = true;
                    }
                    Sem = (Label)this.PnlFrm.Controls.Find("LblTus" + i.ToString(), false)[0];
                    if (Sem.BackColor != Color.LightGray)
                    {
                        Alarm.Tus = true;
                    }
                    Sem = (Label)this.PnlFrm.Controls.Find("LblWen" + i.ToString(), false)[0];
                    if (Sem.BackColor != Color.LightGray)
                    {
                        Alarm.Wed = true;
                    }
                    Sem = (Label)this.PnlFrm.Controls.Find("LblThu" + i.ToString(), false)[0];
                    if (Sem.BackColor != Color.LightGray)
                    {
                        Alarm.Thu = true;
                    }
                    Sem = (Label)this.PnlFrm.Controls.Find("LblFri" + i.ToString(), false)[0];
                    if (Sem.BackColor != Color.LightGray)
                    {
                        Alarm.Fri = true;
                    }
                    Sem = (Label)this.PnlFrm.Controls.Find("LblSat" + i.ToString(), false)[0];
                    if (Sem.BackColor != Color.LightGray)
                    {
                        Alarm.Sat = true;
                    }
                    NumericUpDown updn = (NumericUpDown)this.PnlFrm.Controls.Find("UpDownHr" + i.ToString(), false)[0];
                    Alarm.Hours = int.Parse(updn.Value.ToString());
                    updn = (NumericUpDown)this.PnlFrm.Controls.Find("UpDownMn" + i.ToString(), false)[0];
                    Alarm.Minutes = int.Parse(updn.Value.ToString());
                    Thread.Sleep(50);
                    bool result = SharedLib.ModemGSM.AlarmsWrite(Alarm);
                    if (result)
                    {
                        SharedLib.ShowMsgApp("");
                    }
                    else
                    {
                        SharedLib.ModemGSM.SetError(ResourcesText.ErrAlarm + " " + Alarm.Index.ToString());
                        SharedLib.ShowMsgApp(SharedLib.ModemGSM.Error);
                        break;
                    }
                }
            }
            this.Cursor = Cursors.Default;
            SharedLib.EndWaitProcessThread();
            this.Close();
        }

        private void CboRep_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox Cbo = (ComboBox)sender;
            AlarmsRepeate[int.Parse(Cbo.Tag.ToString())-1] = Cbo.SelectedIndex;
            if (Cbo.SelectedIndex != 2)
            {
                Label LblSem = (Label)this.PnlFrm.Controls.Find("LblSun" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
                LblSem = (Label)this.PnlFrm.Controls.Find("LblSun" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
                LblSem = (Label)this.PnlFrm.Controls.Find("LblMon" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
                LblSem = (Label)this.PnlFrm.Controls.Find("LblTus" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
                LblSem = (Label)this.PnlFrm.Controls.Find("LblWen" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
                LblSem = (Label)this.PnlFrm.Controls.Find("LblThu" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
                LblSem = (Label)this.PnlFrm.Controls.Find("LblFri" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
                LblSem = (Label)this.PnlFrm.Controls.Find("LblSat" + Cbo.Tag.ToString(), false)[0];
                LblSem.BackColor = Color.LightGray;
            }
        }
    }
}
